package com.tzz.controller;

import com.tzz.entity.HttpResult;
import com.tzz.entity.Product;
import com.tzz.exception.AddExistException;
import com.tzz.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/pre/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @PostMapping("/query")
    public HttpResult<List<Product>> findProducts(@RequestBody Product product){
        return HttpResult.success(productService.findProductsByIdName(product));
    }

    @PostMapping("/add")
    public HttpResult<Product> addProduct(@RequestBody @Validated Product product) throws AddExistException {
        productService.addProduct(product);
        return HttpResult.success(product);
    }

    @PostMapping("/query/v2")
    public void queryV2(@RequestBody List skuIds){
        System.out.println(skuIds);
//        System.out.println(String.valueOf(JSONObject.parse(skuIds).get("skuIds")));
    }

    @PostMapping("/delete")
    public HttpResult<Object> deleteProduct(@RequestBody HashMap<String,Integer> id){
        productService.deleteProductById(id.get("id"));
        return HttpResult.success(id);
    }

}
